﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using HTML5Converter.Entities;

namespace HTML5Converter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<ReplaceRemove> lstRemReplac = new List<ReplaceRemove>();
        List<AddContent> lstAddContent = new List<AddContent>();
        public MainWindow()
        {
            InitializeComponent();
            //this.Width = (SystemParameters.MaximizedPrimaryScreenWidth - 6.0 - 2 * SystemParameters.BorderWidth) - 10;
            //this.Height = (SystemParameters.MaximizedPrimaryScreenHeight - 6.0 - 2 * SystemParameters.BorderWidth) - 10;
            //this.Left = 0.0;
            //this.Top = 0.0;
            if (new FileInfo(App.Html5RulesFilePath).Exists)
                LoadXmlData();
            ListReplaceRemove = lstRemReplac;
            ListAddContent = lstAddContent;
        }

        public void LoadXmlData()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(App.Html5RulesFilePath);
            XmlNodeList nodLst = doc.SelectNodes("//ReplaceRemove");
            foreach (XmlNode nod in nodLst)
            {
                lstRemReplac.Add(new ReplaceRemove()
                {
                    StartsWith = nod.SelectSingleNode("StartsWith").InnerText,
                    Contains = nod.SelectSingleNode("Contains").InnerText,
                    ReplaceWith = nod.SelectSingleNode("ReplaceWith").InnerText,
                    ReplaceRelativeTags = bool.Parse(nod.SelectSingleNode("ReplaceRelativeTags").InnerText),
                    Where = (WhereEnum)nod.SelectSingleNode("Where").InnerText[0],
                    Action = (ActionEnum)nod.SelectSingleNode("Action").InnerText[0]
                });
            }

            nodLst = doc.SelectNodes("//AddContnet");
            foreach (XmlNode nod in nodLst)
            {
                lstAddContent.Add(new AddContent()
                {
                    Content = nod.SelectSingleNode("Content").InnerText,
                    ContentType = (ContentTypeEnum)nod.SelectSingleNode("ContentType").InnerText[0],
                    Where = (WhereEnum)nod.SelectSingleNode("Where").InnerText[0]
                });

            }
        }

        private void btnConverter_Click(object sender, RoutedEventArgs e)
        {
            UC_Converter objConverter = new UC_Converter();
            GrdContent.Children.Clear();
            GrdContent.Children.Add(objConverter);
        }

        private void btnReplaceRemoveContent_Click(object sender, RoutedEventArgs e)
        {
            UC_ReplaceRemove objAddEditRules = new UC_ReplaceRemove();
            GrdContent.Children.Clear();
            GrdContent.Children.Add(objAddEditRules);
        }

        private void btnAddContent_Click(object sender, RoutedEventArgs e)
        {
            UC_AddContent objAddContent = new UC_AddContent();
            GrdContent.Children.Clear();
            GrdContent.Children.Add(objAddContent);
        }

        public List<ReplaceRemove> ListReplaceRemove { get; set; }

        public List<AddContent> ListAddContent { get; set; }
    }
}
